import torch
import torch.nn as nn

class SimpleAttnSleep(nn.Module):
    def __init__(self, input_dim, hidden_dim, num_classes):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv1d(input_dim, hidden_dim, kernel_size=5, padding=2),
            nn.ReLU(),
            nn.MaxPool1d(2)
        )
        self.attention = nn.Linear(hidden_dim, 1)
        self.fc = nn.Linear(hidden_dim, num_classes)

    def forward(self, x):
        # x: (batch_size, 1, seq_len)
        x = self.conv(x)  # (batch_size, hidden_dim, seq_len/2)
        x = x.transpose(1, 2)  # (batch_size, seq_len/2, hidden_dim)
        
        # 注意力机制
        attn_weights = torch.softmax(self.attention(x), dim=1)
        x = (x * attn_weights).sum(dim=1)  # (batch_size, hidden_dim)
        
        return self.fc(x)